﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obout.Grid;
using HostingDomainWebsite;

namespace Hosting
{
    public partial class OrderManagement : System.Web.UI.Page
    {

        protected SiteMaster master;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
            }
            master = Master as SiteMaster;
            master.setCurrentNav("OrderManagement");
        }

        #region [Insert Order]
        protected void gOrder_InsertCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.Orders order = new Class.Orders();
            var isValid = order.addValid(e.Record["ClientName"].ToString(), e.Record["ClientEmail"].ToString(), e.Record["ClientPhone"].ToString()
                    , e.Record["ClientAddress"].ToString());

            if (isValid == 1)
            {
                var isInsert = order.add(e.Record["ClientName"].ToString(), e.Record["ClientEmail"].ToString(), e.Record["ClientGender"].ToString(), e.Record["ClientPhone"].ToString()
                    , e.Record["ClientAddress"].ToString(), Boolean.Parse(e.Record["IsDone"].ToString()));

                if (isInsert == 0)
                {
                    master.getMessageSuccess("A new Order has been created!");
                }
                else if (isInsert == -99)
                {
                    master.getMessageError("There is an error when insert a new Order!");
                }
            }
            else if (isValid == 10)
            {
                master.getMessageAttention("Customer name is null or empty");
            }
            else if (isValid == 20)
            {
                master.getMessageAttention("Customer email is null or empty");
            }
            else if (isValid == 21)
            {
                master.getMessageAttention("Customer email is not valid");
            }
            else if (isValid == 30)
            {
                master.getMessageAttention("Customer phone is null or empty");
            }
            else if (isValid == 31)
            {
                master.getMessageAttention("Customer phone is not valid");
            }
            else if (isValid == 40)
            {
                master.getMessageAttention("Customer address is null or empty");
            }
        }
        #endregion

        #region [Update Order]
        protected void gOrder_UpdateCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.Orders order = new Class.Orders();
            var isValid = order.editValid(e.Record["OId"].ToString(), e.Record["ClientName"].ToString(), e.Record["ClientEmail"].ToString(), e.Record["ClientPhone"].ToString()
                    , e.Record["ClientAddress"].ToString());

            if (isValid == 1)
            {
                var isUpdate = order.edit(e.Record["OId"].ToString(), e.Record["ClientName"].ToString(), e.Record["ClientEmail"].ToString(), e.Record["ClientGender"].ToString()
                    , e.Record["ClientPhone"].ToString(), e.Record["ClientAddress"].ToString(), Boolean.Parse(e.Record["IsDone"].ToString()));

                if (isUpdate == 1)
                {
                    master.getMessageSuccess("A new Order has been created!");
                }
                else if (isUpdate == 0)
                {
                    master.getMessageAttention("This Order has been done, you cannot update!");
                }
                else if (isUpdate == -99)
                {
                    master.getMessageError("There is an error when insert a new Order!");
                }
            }
            else if (isValid == 2)
            {
                master.getMessageAttention("Order ID is null or empty");
            }
            else if (isValid == 3)
            {
                master.getMessageAttention("Order ID is not valid");
            }
            else if (isValid == 10)
            {
                master.getMessageAttention("Customer name is null or empty");
            }
            else if (isValid == 20)
            {
                master.getMessageAttention("Customer email is null or empty");
            }
            else if (isValid == 21)
            {
                master.getMessageAttention("Customer email is not valid");
            }
            else if (isValid == 30)
            {
                master.getMessageAttention("Customer phone is null or empty");
            }
            else if (isValid == 31)
            {
                master.getMessageAttention("Customer phone is not valid");
            }
            else if (isValid == 40)
            {
                master.getMessageAttention("Customer address is null or empty");
            }
        }
        #endregion

        #region [Delete Order]
        protected void gOrder_DeleteCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.Orders order = new Class.Orders();
            var isDelete = order.delete(e.Record["OId"].ToString());
            if(isDelete == 1)
            {
                master.getMessageSuccess("Delete successfully!");
            }
            else if (isDelete == 0)
            {
                master.getMessageAttention("This Order has been done, you cannot delete!");
            }
            else if (isDelete == -99)
            {
                master.getMessageAttention("There is an error when delete this Order!");
            }
        }
        #endregion

        #region [Insert Order Detail]
        protected void dgOrderDetail_InsertCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.OrderDetail detail = new Class.OrderDetail();
            var isValid = detail.addValid(e.Record["OId"].ToString());
            if (isValid == 1)
            {
                var isInsert = detail.add(e.Record["OId"].ToString(), e.Record["ServiceDetail"].ToString());
                if(isInsert == 1)
                {
                    master.getMessageSuccess("Insert a new Order Detail successfully!");
                }
                else if (isInsert == 0)
                {
                    master.getMessageAttention("Order contains this Order Detail has been done, you cannot insert another Order Detail!");
                }
                else if (isInsert == -99)
                {
                    master.getMessageError("There is an error when insert new Order Detail!");
                }
            }
            else if (isValid == 2)
            {
                master.getMessageAttention("Order ID is null or empty");
            }
            else if (isValid == 3)
            {
                master.getMessageAttention("Order ID is not valid");
            }
        }
        #endregion

        #region [Update Order Detail]
        protected void dgOrderDetail_UpdateCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.OrderDetail detail = new Class.OrderDetail();
            var isValid = detail.editValid(e.Record["ODId"].ToString(), e.Record["OId"].ToString());
            if (isValid == 1)
            {
                var isInsert = detail.edit(e.Record["ODId"].ToString(), e.Record["OId"].ToString(), e.Record["ServiceDetail"].ToString());
                if (isInsert == 1)
                {
                    master.getMessageSuccess("Update Order Detail successfully!");
                }
                else if (isInsert == 0)
                {
                    master.getMessageAttention("Order contains this Order Detail has been done, you cannot update this Order Detail!");
                }
                else if (isInsert == -99)
                {
                    master.getMessageError("There is an error when update Order Detail!");
                }
            }
            else if (isValid == 2)
            {
                master.getMessageAttention("Order Detail ID is null or empty");
            }
            else if (isValid == 3)
            {
                master.getMessageAttention("Order Detail ID is not valid");
            }
            else if (isValid == 4)
            {
                master.getMessageAttention("Order ID is null or empty");
            }
            else if (isValid == 5)
            {
                master.getMessageAttention("Order ID is not valid");
            }
        }
        #endregion

        #region [Delete Order Detail]
        protected void dgOrderDetail_DeleteCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.OrderDetail detail = new Hosting.Class.OrderDetail();
            var isDelete = detail.delete(e.Record["ODId"].ToString(), e.Record["OId"].ToString());
            if (isDelete == 1)
            {
                master.getMessageSuccess("Delete Order Detail successfully!");
            }
            else if (isDelete == 0)
            {
                master.getMessageAttention("Order contains this Order Detail has been done, you cannot delete this Order Detail!");
            }
            else if (isDelete == -99)
            {
                master.getMessageError("There is an error when delete Order Detail!");
            }
        }
        #endregion
    }
}